package cn.evilelf.sort;

public class Shell {

	public static int[] sort(int array[]) {
		for (int gap = array.length / 2; gap > 0; gap /= 2) {
			for (int i = gap; i < array.length; ++i) {
				int tmp = array[i];
				int j;
				for (j = i; j >= gap && array[j - gap] > tmp; j -= gap) {
					array[j] = array[j - gap];
				}
				array[j] = tmp;
			}
		}

		return array;
	}

	public static long[] sort(long array[]) {
		for (int gap = array.length / 2; gap > 0; gap /= 2) {
			for (int i = gap; i < array.length; ++i) {
				long tmp = array[i];
				int j;
				for (j = i; j >= gap && array[j - gap] > tmp; j -= gap) {
					array[j] = array[j - gap];
				}
				array[j] = tmp;
			}
		}

		return array;
	}

	public static float[] sort(float array[]) {
		for (int gap = array.length / 2; gap > 0; gap /= 2) {
			for (int i = gap; i < array.length; ++i) {
				float tmp = array[i];
				int j;
				for (j = i; j >= gap && array[j - gap] > tmp; j -= gap) {
					array[j] = array[j - gap];
				}
				array[j] = tmp;
			}
		}

		return array;
	}

	public static double[] sort(double array[]) {
		for (int gap = array.length / 2; gap > 0; gap /= 2) {
			for (int i = gap; i < array.length; ++i) {
				double tmp = array[i];
				int j;
				for (j = i; j >= gap && array[j - gap] > tmp; j -= gap) {
					array[j] = array[j - gap];
				}
				array[j] = tmp;
			}
		}

		return array;
	}

	@SuppressWarnings("unchecked")
	public static Comparable[] sort(Comparable array[]) {
		for (int gap = array.length / 2; gap > 0; gap /= 2) {
			for (int i = gap; i < array.length; ++i) {
				Comparable tmp = array[i];
				int j;
				for (j = i; j >= gap && array[j - gap].compareTo(tmp) > 0; j -= gap) {
					array[j] = array[j - gap];
				}
				array[j] = tmp;
			}
		}

		return array;
	}

	public static void main(String[] args) {
		String[] array = { "bad", "mum", "fuck", "love", "chuanshiji", "from" };
		// int[] array = { 2, 13, 6, 11, 3, 6 };
		sort(array);
		for (int i = 0; i < array.length; ++i) {
			System.out.println(array[i]);
		}

		// System.out.println("fuck".compareTo("chuanshiji"));
	}
}
